﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace LanAnh
{
    class MyDataPaging
    {
        Data dbData = new Data();
        #region Properties
        private String sSqlComplete;

        public String SSqlComplete
        {
            get { return sSqlComplete; }
            set { sSqlComplete = value; }
        }

        private String _order;

        public String Order
        {
            get { return _order; }
            set { _order = value; }
        }

        private String _Sql;
        public String Sql
        {
            get { return _Sql; }
            set { _Sql = value; }
        }

        private Int64 _iTotalItems;

        public Int64 ITotalItems
        {
            get { return _iTotalItems; }
            set { _iTotalItems = value; }
        }
        private int _iLow;

        public int ILow
        {
            get { return _iLow; }
            set { _iLow = value; }
        }
        private int _iHigh;

        public int IHigh
        {
            get { return _iHigh; }
            set { _iHigh = value; }
        }
        private String _sfilter;

        public String Sfilter
        {
            get { return _sfilter; }
            set { _sfilter = value; }
        }
        private int _currentPage;

        public int CurrentPage
        {
            get { return _currentPage; }
            set { _currentPage = value; }
        }
        private Int64 _iTotalPages;

        public Int64 ITotalPages
        {
            get
            {
                if (ITotalItems % IItemPeerPage == 0)
                {
                    _iTotalPages = ITotalItems / IItemPeerPage;
                }
                else
                {
                    _iTotalPages = ITotalItems / IItemPeerPage + 1;
                }
                return _iTotalPages;
            }
        }
        private int _iItemPeerPage;

        public int IItemPeerPage
        {
            get { return _iItemPeerPage; }
            set { _iItemPeerPage = value; }
        }
        private String _table;

        public String Table
        {
            get { return _table; }
            set { _table = value; }
        }


        #endregion
        DataGridView Datagridview;
        TextBox Tstbtrang;
        DevComponents.DotNetBar.LabelX LblTrang;
        public MyDataPaging(String table, String sSQL, String sFilter, String sOrder, int iItemsPeerPage, DataGridView dgview, TextBox tstbtrang, DevComponents.DotNetBar.LabelX lbltrang)
        {
            this.Table = table;
            this.Sql = sSQL;
            this.Sfilter = sFilter;
            this.Order = sOrder;
            this.IItemPeerPage = iItemsPeerPage;
            this.Datagridview = dgview;
            this.Tstbtrang = tstbtrang;
            this.LblTrang = lbltrang;
            this.ITotalItems = CountNumber(Table);
            Tstbtrang.Text = IItemPeerPage.ToString();
            LblTrang.Text = ITotalPages.ToString();
        }
        public Int64 CountNumber(string table)
        {
            string sqlstring = "select count(*) from " + table + " " + this.Sfilter;
            try
            {
                return Int64.Parse(dbData.executeQueryTable(sqlstring).Rows[0][0].ToString());
            }
            catch
            {
                return 1;
            }
        }
        public bool Update(string filter)
        {
            try
            {
                Sfilter = filter;
                this.ITotalItems = CountNumber(Table);
                LblTrang.Text = ITotalPages.ToString();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Update(int item)
        {
            this.IItemPeerPage = item;
            ITotalItems = CountNumber(Table);
            LblTrang.Text = ITotalPages.ToString();
            return true;
        }

        private DataTable Paging(Int64 low, Int64 high)
        {
            //Status.TrangThai("Đang phân trang....");
            int pagenow = (Convert.ToInt16(Tstbtrang.Text));
            sSqlComplete = @"SELECT * FROM (select *, ROW_NUMBER() OVER (ORDER BY " + Order + ") rownum from " +
                           "(" + Sql + " " + Sfilter + ") sq ) " +
                            "sq WHERE sq.rownum BETWEEN " + low + " AND " + high;

            // "(SELECT * FROM (select *, ROW_NUMBER() OVER (ORDER BY " + Order + ") rownum from " + Sql + Sfilter + ")sq) sq WHERE sq.rownum BETWEEN " + low + " AND " + high + " )";
            try
            {
                DataTable temp = dbData.executeQueryTable(sSqlComplete);
                return temp;
            }
            catch {
                return null;
            }
            //Status.Complete();
            
        }
        #region Method
        public void Previous()
        {
            int pagenow = (Convert.ToInt16(Tstbtrang.Text));
            if (pagenow > 1)
            {
                Datagridview.DataSource = Paging(((pagenow - 2) * IItemPeerPage + 1), ((pagenow - 1) * IItemPeerPage));
                Tstbtrang.Text = (pagenow - 1).ToString();
            }
        }

        public void GoIndex()
        {
            int pagenow = (Convert.ToInt16(Tstbtrang.Text));
            if ((pagenow >= 1) && (pagenow <= _iTotalPages))
            {
                Datagridview.DataSource = Paging(((pagenow - 1) * IItemPeerPage + 1), ((pagenow) * IItemPeerPage));
                Tstbtrang.Text = (pagenow).ToString();
            }
        }
        public void Next()
        {
            int pagenow = (Convert.ToInt16(Tstbtrang.Text));
            if (pagenow < _iTotalPages)
            {
                Datagridview.DataSource = Paging(((pagenow) * IItemPeerPage + 1), ((pagenow + 1) * IItemPeerPage));
                Tstbtrang.Text = (pagenow + 1).ToString();
            }
        }
        public void Home()
        {
            int pagenow = (Convert.ToInt16(Tstbtrang.Text));
            Datagridview.DataSource = Paging(1, IItemPeerPage);
            Tstbtrang.Text = "1";
        }
        public void End()
        {
            Int64 pagenow = (Int64.Parse(Tstbtrang.Text));
            Datagridview.DataSource = Paging((ITotalPages - 1) * IItemPeerPage + 1, ITotalPages * IItemPeerPage);
            Tstbtrang.Text = ITotalPages.ToString();
        }
        public void Refresh()
        {
            int pagenow = (Convert.ToInt16(Tstbtrang.Text));
            pagenow--;
            if (pagenow < _iTotalPages)
            {
                Datagridview.DataSource = Paging(((pagenow) * IItemPeerPage + 1), ((pagenow + 1) * IItemPeerPage));
                Tstbtrang.Text = (pagenow + 1).ToString();
            }
        }
        #endregion
    }
}